package com.mcflies.ui.listView.handlerAndListView;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.mcflies.ui.R;
import com.mcflies.ui.listView.McfliesBaseListViewAdapter;

public class HandlerAndListViewMainActivity extends Activity implements
		OnScrollListener {
	private ListView androidListViewMain_LV;
	private LinearLayout loading_LL;
	private ProgressBar loading_PB;
	private McfliesBaseListViewAdapter adapter;
	private List<String> dataList = new ArrayList<String>();

	private Thread mThread;

	private boolean isExistNewData = true;

	/*
	 * 设置布局显示属性
	 */
	private LayoutParams wrapLayoutParams = new LayoutParams(
			LinearLayout.LayoutParams.WRAP_CONTENT,
			LinearLayout.LayoutParams.WRAP_CONTENT);

	private LayoutParams fillLayoutParams = new LayoutParams(
			LinearLayout.LayoutParams.FILL_PARENT,
			LinearLayout.LayoutParams.FILL_PARENT);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.android_listview_main);
		initWidget();
		for (int i = 0; i < 10; i++) {
			dataList.add("HandlerAndListViewMainActivity测试数据" + (i + 1));
		}
		adapter = new McfliesBaseListViewAdapter(dataList, this);
		// androidListViewMain_LV.addFooterView(loading_LL);
		androidListViewMain_LV.setAdapter(adapter);
		androidListViewMain_LV.setOnScrollListener(this);
		androidListViewMain_LV.setTextFilterEnabled(true);
	}

	private void initWidget() {
		androidListViewMain_LV = (ListView) findViewById(R.id.androidListViewMain_LV);
		LinearLayout layout = new LinearLayout(this);
		layout.setOrientation(LinearLayout.HORIZONTAL);
		loading_PB = new ProgressBar(this);
		layout.addView(loading_PB, wrapLayoutParams);
		TextView loading_TV = new TextView(this);
		loading_TV.setText("数据加载中...");
		loading_TV.setGravity(Gravity.CENTER_VERTICAL);
		layout.addView(loading_TV, fillLayoutParams);
		layout.setGravity(Gravity.CENTER);
		loading_LL = new LinearLayout(this);
		loading_LL.addView(layout, wrapLayoutParams);
		loading_LL.setGravity(Gravity.CENTER);
		loading_LL.setVisibility(View.GONE);
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		Toast.makeText(this, "0000000000", Toast.LENGTH_SHORT).show();
		if (firstVisibleItem + visibleItemCount == totalItemCount) {
			if (null == mThread || !mThread.isAlive()) {
				androidListViewMain_LV.addFooterView(loading_LL);
				mThread = new Thread() {
					@Override
					public void run() {
						if (isExistNewData) {
							for (int i = 0; i < 10; i++) {
								dataList.add("新增测试数据：" + (i + 1));
							}
							Message msg = new Message();
							msg.what = 1;
							handler.sendMessage(msg);
						}
					}
				};
				handler.post(mThread);
				isExistNewData = false;
				mThread.start();
			}
		}
	}

	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if (1 == msg.what) {
				androidListViewMain_LV.removeFooterView(loading_LL);
				mThread.stop();
				adapter.notifyDataSetChanged();
			}
		};
	};

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {

	}

}
